package wqh.dao.impl;

import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import wqh.dao.GeoLocationDao;
import wqh.domain.GeoLocation;

import java.util.List;

/**
 * Created by devts on 2017/1/3.
 */
@Repository("geoLocationDao")
public class GeoLocationDaoImpl extends DaoImpl<GeoLocation> implements GeoLocationDao {

    public int findMaxLevel() {
        Session session = this.getSessionFactory().getCurrentSession();
        String hql = "SELECT MAX(g.level) FROM GeoLocation g";
        Query query = session.createQuery(hql);
        return  query.uniqueResult() == null ? 0 : (Integer) query.uniqueResult();
    }

    public List<GeoLocation> findByParentID(Long parent_id) {
        Session session = this.getSessionFactory().getCurrentSession();
        String hql = "FROM GeoLocation g WHERE g.parent_id = :parentID";
        Query query = session.createQuery(hql);
        query.setParameter("parentID",parent_id);
        return query.list();
    }

    public void deleteByParentIdAndLevel(Long parent_id, Integer level) {
        Session session = this.getSessionFactory().getCurrentSession();
        String hql = "DELETE FROM GeoLocation g WHERE g.parent_id = :parentID AND g.level = :Level";
        Query query = session.createQuery(hql);
        query.setParameter("parentID",parent_id);
        query.setParameter("Level",level);
        int count = query.executeUpdate();
        System.out.println("delete:" +count);
    }
}
